<template>
  <el-dialog
    :title="!dataForm.buyId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="房源id" prop="buyBulidingId">
      <el-input v-model="dataForm.buyBulidingId" placeholder="房源id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="buyUserId">
      <el-input v-model="dataForm.buyUserId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="用户名称" prop="buyUserName">
      <el-input v-model="dataForm.buyUserName" placeholder="用户名称"></el-input>
    </el-form-item>
    <el-form-item label="房源名称" prop="buyBulidingName">
      <el-input v-model="dataForm.buyBulidingName" placeholder="房源名称"></el-input>
    </el-form-item>
    <el-form-item label="房源地址" prop="buyBulidingAddr">
      <el-input v-model="dataForm.buyBulidingAddr" placeholder="房源地址"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="buyPrice">
      <el-input v-model="dataForm.buyPrice" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="总价" prop="buySumPrice">
      <el-input v-model="dataForm.buySumPrice" placeholder="总价"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          buyId: 0,
          buyBulidingId: '',
          buyUserId: '',
          buyUserName: '',
          buyBulidingName: '',
          buyBulidingAddr: '',
          buyPrice: '',
          buySumPrice: ''
        },
        dataRule: {
          buyBulidingId: [
            { required: true, message: '房源id不能为空', trigger: 'blur' }
          ],
          buyUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          buyUserName: [
            { required: true, message: '用户名称不能为空', trigger: 'blur' }
          ],
          buyBulidingName: [
            { required: true, message: '房源名称不能为空', trigger: 'blur' }
          ],
          buyBulidingAddr: [
            { required: true, message: '房源地址不能为空', trigger: 'blur' }
          ],
          buyPrice: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          buySumPrice: [
            { required: true, message: '总价不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.buyId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.buyId) {
            this.$http({
              url: this.$http.adornUrl(`/other/buy/info/${this.dataForm.buyId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.buyBulidingId = data.buy.buyBulidingId
                this.dataForm.buyUserId = data.buy.buyUserId
                this.dataForm.buyUserName = data.buy.buyUserName
                this.dataForm.buyBulidingName = data.buy.buyBulidingName
                this.dataForm.buyBulidingAddr = data.buy.buyBulidingAddr
                this.dataForm.buyPrice = data.buy.buyPrice
                this.dataForm.buySumPrice = data.buy.buySumPrice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/other/buy/${!this.dataForm.buyId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'buyId': this.dataForm.buyId || undefined,
                'buyBulidingId': this.dataForm.buyBulidingId,
                'buyUserId': this.dataForm.buyUserId,
                'buyUserName': this.dataForm.buyUserName,
                'buyBulidingName': this.dataForm.buyBulidingName,
                'buyBulidingAddr': this.dataForm.buyBulidingAddr,
                'buyPrice': this.dataForm.buyPrice,
                'buySumPrice': this.dataForm.buySumPrice
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
